package goodbaby.dklt.bluetoothutils;

import android.util.Log;

import com.classic.core.utils.DateUtil;

import goodbaby.dklt.GbbPreferences;


/**
 * Created by Jayden on 2016/5/16 17:21.
 *
 * @version V1.0
 * @Title: DeviceNumberUtils
 * @Description: 蓝牙设备底层返回数据解析工具类
 */
public final class DeviceNumberUtils {

    /**
     * 根据蓝牙底层返回数据解析获得步数
     *
     * @param stepStr
     * @return stepNum
     */
    public static int stepNumber(String stepStr) {
        String currentStr = stepStr.substring(34, 130);
        int stepNum = 0;
        for (int i = 0; i < currentStr.length() - 3; i = i + 4) {
            stepNum += Integer.parseInt(currentStr.substring(i, i + 4), 16);
        }
        return stepNum;
    }

    public static int stepNumber(byte[] stepData) {
//        char[] data = getChars(stepData);
        int stepNum = 0;
        for (int i = 17; i < stepData.length - 1; i = i + 2) {
            int a = ((int) stepData[i] & 0xff) << 8;
            int b = stepData[i + 1] & 0xff;
            stepNum += a + b;
        }
        return stepNum;
    }

    /**
     * 根据蓝牙底层返回数据解析获得电量
     *
     * @param eleStr
     * @return eleNum
     */
    public static int eleNum(String eleStr) {
        String currentStr = eleStr.substring(26, 28);
        int eleNum = 0;
        eleNum = Integer.parseInt(currentStr, 16);
        return eleNum;
    }

    public static int eleNum(byte[] eleData) {
        int eleNum = 0;
        eleNum = eleData[13] & 0xff;
        return eleNum;
    }

    /**
     * 根据蓝牙底层返回数据解析获得睡眠情况
     *
     * @param sleepData
     */
    public static void setSleepInfo(byte[] sleepData) {
        int deepTimes = 0;
        int lightTimes = 0;
        int[] sleepMode = new int[10];
        int sleepHour = 0;
        int sleepMin = 0;
        int startHour = 0;
        int startMin = 0;
        int endHour = 0;
        int endMin = 0;
        int preHour = 0;
        int preMin = 0;
        for (int i = 16; i < sleepData.length - 1; i = i + 2) {
            sleepHour = ((int) sleepData[i] & 0xf8) >> 3;
            int minA = ((int) sleepData[i] & 0x07) << 3;
            int minB = ((int) sleepData[i + 1] & 0xe0) >> 5;
            sleepMin = minA + minB;
            sleepMode[i / 2 - 8] = (int) sleepData[i + 1] & 0x0f;
            Log.e("sleep", sleepHour + "+" + sleepMin + "+" + sleepMode);
            if (sleepMode[i / 2 - 8] == 1) {
                if (startHour == 0 && startMin == 0) {
                    startHour = sleepHour;
                    startMin = sleepMin;
                }
            } else if (sleepMode[i / 2 - 8] == 2) {
                if (preHour == 0 && preMin == 0) {
                    preHour = sleepHour;
                    preMin = sleepMin;
                    startHour = sleepHour;
                    startMin = sleepMin;
                } else {
                    if (sleepMode[i / 2 - 9] == 2) {
                        lightTimes += (sleepHour - preHour) * 60 + sleepMin - preMin;
                    } else if (sleepMode[i / 2 - 9] == 3) {
                        deepTimes += (sleepHour - preHour) * 60 + sleepMin - preMin;
                    }
                    preHour = sleepHour;
                    preMin = sleepMin;
                }
            } else if (sleepMode[i / 2 - 8] == 3) {
                if (preHour == 0 && preMin == 0) {
                    preHour = sleepHour;
                    preMin = sleepMin;
                    startHour = sleepHour;
                    startMin = sleepMin;
                } else {
                    if (sleepMode[i / 2 - 9] == 2) {
                        lightTimes += (sleepHour - preHour) * 60 + sleepMin - preMin;
                    } else if (sleepMode[i / 2 - 9] == 3) {
                        deepTimes += (sleepHour - preHour) * 60 + sleepMin - preMin;
                    }
                    preHour = sleepHour;
                    preMin = sleepMin;
                }
            } else if (sleepMode[i / 2 - 8] == 4) {
                if (preHour == 0 && preMin == 0) {
                    preHour = sleepHour;
                    preMin = sleepMin;
                    startHour = sleepHour;
                    startMin = sleepMin;
                } else {
                    if (sleepMode[i / 2 - 9] == 2) {
                        lightTimes += (sleepHour - preHour) * 60 + sleepMin - preMin;
                    } else if (sleepMode[i / 2 - 9] == 3) {
                        deepTimes += (sleepHour - preHour) * 60 + sleepMin - preMin;
                    }
                    preHour = sleepHour;
                    preMin = sleepMin;
                }
            } else if (sleepMode[i / 2 - 8] == 5) {
                if (sleepMode[i / 2 - 9] == 2) {
                    lightTimes += (sleepHour - preHour) * 60 + sleepMin - preMin;
                } else if (sleepMode[i / 2 - 9] == 3) {
                    deepTimes += (sleepHour - preHour) * 60 + sleepMin - preMin;
                }
                endHour = sleepHour;
                endMin = sleepMin;
            }
        }
        GbbPreferences.saveDeepSleep(deepTimes + "");
        GbbPreferences.saveLightSleep(lightTimes + "");
        GbbPreferences.saveStartTime(DateUtil.formatTime(startHour, startMin));
        GbbPreferences.saveEndTime(DateUtil.formatTime(endHour, endMin));
    }

}
